package com.douxg.fastCoding.db;

import cn.hutool.extra.spring.SpringUtil;
import com.douxg.fastCoding.spring.consts.BizCode;
import com.douxg.fastCoding.spring.exception.BizRuntimeException;
import com.douxg.fastCoding.web.consts.DbType;

import java.util.Optional;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/7/14 22:53
 */
public class DbFactory {
    public static DbFunction getDbFunction(String dbTypeName) {
        DbFunction dbFunction = null;
        try {
            dbFunction = SpringUtil.getBean(dbTypeName.concat("Function"), DbFunction.class);
        } catch (Exception e) {
        }
        if (dbFunction == null) {
            dbFunction = Optional.ofNullable(DbType.getByName(dbTypeName)).map(dbType -> dbType.getDbFunction()).orElse(null);
            if (dbFunction == null) {
                throw new BizRuntimeException(BizCode.DBTYPE_NO_EXIST);
            }
        }
        return dbFunction;
    }
}
